﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;

namespace WFDataBaseProject.Controls
{
    /// <summary>
    /// Interaction logic for UCQueryEditor.xaml
    /// </summary>
    public partial class UCQueryEditor : UserControl
    {
        public UCQueryEditor()
        {
            InitializeComponent();
        }



        public string Query
        {
            get { return (string)GetValue(QueryProperty); }
            set { SetValue(QueryProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Query.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty QueryProperty =
            DependencyProperty.Register("Query", typeof(string), typeof(UCQueryEditor), new UIPropertyMetadata(string.Empty));

        private void btnTestQuery_Click(object sender, RoutedEventArgs e)
        {
            switch (Communicator.ConnectionType)
            {
                case WFDataBaseProject.Helpers.TypeOfConnection.SqlServerConnection:
                    using (SqlConnection sqlconnection = new SqlConnection(Communicator.ConnectionString))
                    {
                        try
                        {
                            sqlconnection.Open();
                            SqlCommand sqlcommand = new SqlCommand(txtQuery.Text, sqlconnection);
                            sqlcommand.ExecuteNonQuery();
                            MessageBox.Show("Query parsed successfully");
                        }
                        catch (SqlException caught)
                        {
                            MessageBox.Show("Query parsing failed: {0}", caught.Message);
                        }
                        finally { sqlconnection.Close(); }
                    }
                    break;
                case WFDataBaseProject.Helpers.TypeOfConnection.OledbConnection:
                    using (OleDbConnection oledbconnection = new OleDbConnection(Communicator.ConnectionString))
                    {
                        try
                        {
                            oledbconnection.Open();
                            OleDbCommand oledbcommand = new OleDbCommand(txtQuery.Text, oledbconnection);
                            oledbcommand.ExecuteNonQuery();
                            MessageBox.Show("Query parsed successfully");
                        }
                        catch (OleDbException caught)
                        {
                            MessageBox.Show("Query parsing failed: {0}", caught.Message);
                        }
                        finally { oledbconnection.Close(); }
                    }
                    break;
                case WFDataBaseProject.Helpers.TypeOfConnection.ODBCConnection:
                    using (OdbcConnection odbcconnection = new OdbcConnection(Communicator.ConnectionString))
                    {
                        try
                        {
                            odbcconnection.Open();
                            OdbcCommand odbccommand = new OdbcCommand(txtQuery.Text, odbcconnection);
                            odbccommand.ExecuteNonQuery();
                            MessageBox.Show("Query parsed successfully");
                        }
                        catch (OleDbException caught)
                        {
                            MessageBox.Show("Query parsing failed: {0}", caught.Message);
                        }
                        finally { odbcconnection.Close(); }
                    }
                    break;
                case WFDataBaseProject.Helpers.TypeOfConnection.OtherConnection:
                    throw new NotImplementedException("This is not implemented yet");
                default:
                    throw new NotImplementedException("This is not implemented yet");
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            Query = txtQuery.Text;
            (this.Parent as Window).Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Query = string.Empty;
            (this.Parent as Window).Close();
        }

    }
}
